//实现一个中缀表达式 计算器
public class InfixCalculator {
    public static void main(String[] args) {
        String expression = "4+4-2*2+4/2*4+4-4-4*4/2";
        ArrayStack<Character> operatorStack = new ArrayStack();
        ArrayStack<Integer> numberStack = new ArrayStack<>();
        for (int i = 0; i < expression.length(); i++) {
            char c = expression.charAt(i);
            if (c >= '0' && c <= '9') {
                numberStack.push(c - '0');
            } else if (c == '+' || c == '-') {
                while (!operatorStack.isEmpty()) {
                    process(numberStack, operatorStack);
                }
                operatorStack.push(c);
            } else {
                while (!operatorStack.isEmpty() && (operatorStack.peek() == '*' || operatorStack.peek() == '/')) {
                    process(numberStack, operatorStack);
                }
                operatorStack.push(c);
            }
        }
        while (!operatorStack.isEmpty()) {
            process(numberStack, operatorStack);
        }
        System.out.println(numberStack.peek());
    }

    private static void process(ArrayStack<Integer> numberStack, ArrayStack<Character> operatorStack) {
        char op = operatorStack.pop();
        int num1 = numberStack.pop();
        int num2 = numberStack.pop();
        if (op == '+') {
            numberStack.push(num2 + num1);
        } else if (op == '-') {
            numberStack.push(num2 - num1);
        } else if (op == '*') {
            numberStack.push(num2 * num1);
        } else {
            numberStack.push(num2 / num1);
        }
    }
}